import 'package:flutter/material.dart';
import 'package:hm_show_flutter/api/service/user_service.dart';
import 'package:hm_show_flutter/config/app_build.dart';
import 'package:hm_show_flutter/constant/string.dart';
import 'package:hm_show_flutter/entity/init_entity.dart';
import 'package:hm_show_flutter/utils/navigator_util.dart';
import 'package:hm_show_flutter/utils/shared_preferences_util.dart';
import 'package:shared_preferences/shared_preferences.dart';

///启动页
class SplashPage extends StatefulWidget {
  @override
  _SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  @override
  void initState() {
    super.initState();

    UserService _service = new UserService();
    _service.getInitData((success) {
      setState(() {
        InitEntity entity = success;
        SharedPreferencesUtils.saveInit(entity.data).then((result){
          start();
        });
      });
    }, onFail:(fail) {
      start();
    });
  }

  Future start() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String token = sharedPreferences.getString(Strings.token) ?? null;
    if (token != null) {
      NavigatorUtils.goMallMainPage(context);
      return;
    }
    NavigatorUtils.goLogin(context);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Image.asset(
        AppBuild.app_ic_splash,
        width: double.infinity,
        height: double.infinity,
        fit: BoxFit.fill,
      ),
    );
  }
}
